package cn.gdcp.recyclerview;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

import static android.support.v4.app.ActivityCompat.startActivityForResult;
import static android.support.v4.content.ContextCompat.startActivity;

public class StuAdapter extends RecyclerView.Adapter<StuViewHolder>{
    private Context context;
    private ArrayList<Student> studentArrayList;
    private ArrayList<Student> searchResultList;
    private IOnDelListener listener;
    private boolean isResultList;


    public StuAdapter(Context context,ArrayList<Student> studentArrayList,
                      IOnDelListener listener) {
        this.context=context;
        this.studentArrayList=studentArrayList;
        this.listener=listener;
    }
    public void changeData(ArrayList<Student> studentArrayList){
        this.studentArrayList=studentArrayList;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    /*创建*/
    public StuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.list_item,
                parent,false);
        StuViewHolder viewHolder=new StuViewHolder(view);
        return viewHolder;
    }
    @Override
    /*绑定*/
    public void onBindViewHolder(@NonNull StuViewHolder holder, final int position) {
        final Student s=studentArrayList.get(position);
        holder.headView.setImageResource(s.getImgId());
        holder.nameView.setText(s.getName());
        holder.ageView.setText(String.valueOf(s.getAge()));
        /*列表点击显示信息事件*/
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, s.getName() + "  " + s.getAge(),
                        Toast.LENGTH_SHORT).show();
                //如果是从结果列表中点击信息，编辑页面为结果列表的顺序信息
                /*if(isResultList){
                    Student stu=searchResultList.get(position);
                }
                Intent intent=new Intent();
                intent.setClass(context,EditActivity.class);
                intent.putExtra("NAME",s.getName());
                intent.putExtra("AGE",s.getAge());
                intent.putExtra("IMG",s.getImgId());
                intent.putExtra("NO",s.getNo());
                context.startActivityForResult(intent,1002);*/
            }
        });
         holder.delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(position);
            }
        });
        return;
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }
}
